﻿using PTop.Application.Article;
using PTop.Code;
using PTop.Domain.Entity.Article;
using PTop.Domain.ViewModel.Article;
using System;
using System.Linq;
using System.Web.Mvc;

namespace PTop.Web.Areas.Article.Controllers
{
    public class ArticleController : ControllerBase
    {
        private ArticleApp _app = new ArticleApp();
        private TagApp tagApp = new TagApp();

        [HttpGet]
        [HandlerAuthorize]
        public override ActionResult Index()
        {
            var types = new TrainArticleTypeApp().GetList();
            ViewBag.ArticleType = types.Select(m => new
            {
                Id = m.F_Type,
                Name = m.F_Name
            }).ToJson();
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJson(Pagination pagination, string keyword, int type, int state)
        {
            var data = _app.GetList(pagination, keyword, type, state, -1);
            return PageContent(pagination.records, pagination.rows, data);
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = _app.GetForm(keyValue);
            return Content(data.ToJson());
        }
        /// <summary>
        /// 获取标签
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetTagJson(string keyValue)
        {
            var data = tagApp.GetListByArticle(keyValue).ToArray();
            return Content(data.ToJson());
        }
        /// <summary>
        /// 获取标签
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetTagAllJson(string keyValue)
        {
            var tags = tagApp.GetValidList();
            var data = tagApp.GetListByArticle(keyValue).ToArray();
            var argticleTagList = tags.Select(tag => new ArticleTagModel
            {
                F_TagId = tag.F_Id,
                F_Name = tag.F_Name,
                F_State = tag.F_State,
                F_ArticleId = keyValue,
                F_Select = data.Any(m => m.F_Id == tag.F_Id)
            }).ToList();
            return Content(argticleTagList.ToArray().ToJson());
        }

        /// <summary>
        /// 获取文章分类
        /// </summary>
        /// <returns></returns>
        public ActionResult GetArticleTypes()
        {
            var types = new TrainArticleTypeApp().GetList();
            var rst = types.Select(m => new
            {
                id = m.F_Type,
                text = m.F_Name
            }).ToList();
            return Content(rst.ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult SubmitForm(ArticleEntity articleEntity, string keyValue)
        {
            _app.SubmitForm(articleEntity, keyValue);
            //保存标签
            var tagIdsStr = Request["F_TagIds"];
            var tagIds = new string[0];
            if (!string.IsNullOrEmpty(tagIdsStr))
            {
                tagIds = tagIdsStr.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            }
            new ArticleTagRelationApp().UpdateRelation(articleEntity.F_Id, tagIds);
            return Success("操作成功。");
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            var entity = _app.GetForm(keyValue);
            if (entity == null)
            {
                return Error("文章不存在");
            }
            if (entity.F_Type == 10)
            {
                return Error("说明条款分类的文章不可删除");
            }
            _app.Delete(keyValue);
            return Success("删除成功。");
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize]
        [ValidateAntiForgeryToken]
        public ActionResult Audit(string keyValue, int state)
        {
            _app.Audit(keyValue, state);
            return Success("审核成功。");
        }
    }
}
